package com.avic.rong.search.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.avic.rong.BaseFragment;
import com.avic.rong.MyApplication;
import com.avic.rong.R;
import com.avic.rong.search.MessageEvent;
import com.avic.rong.search.activity.SearchHomePageActivity;
import com.avic.rong.search.adapter.SearchViewHomeJiLuRvAdapter;
import com.avic.rong.search.adapter.SearchViewHomeReMenRvAdapter;
import com.avic.rong.search.api.SearchHotApi;
import com.avic.rong.search.bean.SearchHotBean;
import com.avic.rong.search.db.User;
import com.avic.rong.search.db.UserDao;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.luck.picture.lib.rxbus2.Subscribe;
import com.luck.picture.lib.rxbus2.ThreadMode;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class SearchViewHomeFragment extends BaseFragment implements SearchViewHomeReMenRvAdapter.SearchViewHomeReMenListener, SearchViewHomeJiLuRvAdapter.SearchJiLuListener {

    private View view;
    private RecyclerView rv_ReMen;
    private RecyclerView rv_JiLu;
    private RelativeLayout jiLuRl;
    private List<String> userList = new ArrayList<>();
    private SearchViewHomeReMenRvAdapter reMenRvAdapter;
    private SearchViewHomeJiLuRvAdapter jiLuRvAdapter;
    private ArrayList<SearchHotBean> beans = new ArrayList<>();

    public SearchViewHomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_search_view_home, container, false);
        initView(view);
        initData();
        getData();
        getDao();
//        getDbHelper();

        return view;
    }

//    private void getDbHelper() {
//        SQLiteDatabase db = MyApplication.getInstance().getDbHelper();
//        Cursor cursor = db.query("textjilu", null, null, null, null, null, null);
//        if (cursor.moveToFirst()) {
//            do {
//                // 遍历Cursor对象，取出数据并打印
//                String text = cursor.getString(cursor.getColumnIndex("text"));
//                Log.e("cjx", "jilu text is " + text);
//                list.add(text);
//            } while (cursor.moveToNext());
//        }
//        cursor.close();
//        if (list.size()>0){
//            jiLuRl.setVisibility(View.VISIBLE);
//            rv_JiLu.setVisibility(View.VISIBLE);
//            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),4);
//            rv_JiLu.setLayoutManager(gridLayoutManager);
//            jiLuRvAdapter = new SearchViewHomeJiLuRvAdapter(getContext(),list);
//            rv_JiLu.setAdapter(jiLuRvAdapter);
//        }else {
//            jiLuRl.setVisibility(View.GONE);
//            rv_JiLu.setVisibility(View.GONE);
//        }
//    }

    private void getDao() {
        UserDao userDao = MyApplication.getInstance().getDaoSession().getUserDao();
        List<User> users = userDao.loadAll();
        if (users.size() == 0){
            jiLuRl.setVisibility(View.GONE);
            rv_JiLu.setVisibility(View.GONE);
        }else {
            jiLuRl.setVisibility(View.VISIBLE);
            rv_JiLu.setVisibility(View.VISIBLE);
            GridLayoutManager manager1 = new GridLayoutManager(getContext(),4);
            rv_JiLu.setLayoutManager(manager1);
            for (int i=0;i<users.size();i++){
                userList.add(users.get(i).getText());
            }
            jiLuRvAdapter = new SearchViewHomeJiLuRvAdapter(getContext(),userList);
            rv_JiLu.setAdapter(jiLuRvAdapter);
        }
        Log.e("cjx","user长度："+users.size());

    }

    private void initData() {
        GridLayoutManager manager = new GridLayoutManager(getContext(),2);
        rv_ReMen.setLayoutManager(manager);

        reMenRvAdapter = new SearchViewHomeReMenRvAdapter(getContext());
        reMenRvAdapter.setListener(this);
        rv_ReMen.setAdapter(reMenRvAdapter);

    }

    private void initView(View view) {
        rv_ReMen = (RecyclerView) view.findViewById(R.id.searchView_Home_Rv_ReMen);
        rv_JiLu = (RecyclerView) view.findViewById(R.id.searchView_Home_Rv_JiLu);
        jiLuRl = view.findViewById(R.id.searchView_Home_JiLu_RelativeLayout);
    }


    //获取热搜数据
    private void getData() {
        SearchHotApi hotApi = new SearchHotApi((SearchHomePageActivity)getActivity(), delCartListener);
        hotApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(hotApi);
    }
    //获取热搜数据监听
    HttpOnNextListener delCartListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")){
                Log.e("cjx","热搜："+subjects.getMsg());
                return;
            }
            beans.clear();
            beans.addAll((ArrayList<SearchHotBean>) subjects.getData());
            reMenRvAdapter.setBeans(beans);
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {

        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void addListener() {

    }

    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Subscribe(threadMode = ThreadMode.CURRENT_THREAD)
    public void onEvent(MessageEvent messageEvent){
        Log.e("cjx",messageEvent.getMessage());
    }


    @Override
    public void remenGoResult(String index) {

    }

    @Override
    public void onClickItem(int i) {

    }
}
